﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO.Ports;
using XChipTool.Enties;

namespace XChipTool
{
    public partial class XChip_DeriveOut : DevExpress.XtraEditors.XtraUserControl
    {
        public parameter_exportManager Pe_dal { get; set; }
        public SerialPort sPortTemp;       
        int topIndex = -1;
        public XChip_DeriveOut()
        {
            InitializeComponent();
        }

        private void XChip_DeriveOut_Load(object sender, EventArgs e)
        {
            if ( null != Pe_dal)
            {
                gridControl1.DataSource = Pe_dal.GetList().OrderBy(c => c.SortNum);
                gridView1.RefreshData();
            }
           
        }

        /// <summary>
        /// 点击表格按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void repositoryItemButtonEdit1_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            //获取选中内容进行提示   常考导入文件
            gridView1_RowCellClick(null, null);
            var row = gridView1.GetSelectedRows();
            var ReadAddr = gridView1.GetRowCellValue(row[0], "ReadAddr");
            var addr = gridView1.GetFocusedRowCellDisplayText("addr").ToLower();
            if (addr.Contains("fffd")|| addr.Contains("fffe"))
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("该项不可删除","提示",MessageBoxButtons.OK,MessageBoxIcon.Asterisk);
                return;
            }
            if (idLab.Text == "labelControl7")
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("执行删除操作前，请先选中行", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return;
            }
            
            
            var devMsg =    DevExpress.XtraEditors.XtraMessageBox.Show("是否删除", "删除", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
            if (devMsg == DialogResult.OK) //删除代码
            {                
                Pe_dal.Delete(c=>c.id == int.Parse( idLab.Text));
                //gridControl1.DataSource = Pe_dal.GetList().OrderBy(c => c.SortNum);
            }
            newRefreshDate();
        }

        private void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            int i  = this.gridView1.FocusedRowHandle;
            var id = gridView1.GetRowCellDisplayText(i, gridView1.Columns["id"]);
            var addr = gridView1.GetRowCellDisplayText(i, gridView1.Columns["addr"]);
            var ReadAddr = gridView1.GetRowCellDisplayText(i, gridView1.Columns["ReadAddr"]);
            var RearNote = gridView1.GetRowCellDisplayText(i, gridView1.Columns["RearNote"]);
            var PreNote = gridView1.GetRowCellDisplayText(i, gridView1.Columns["PreNote"]);
            var Module = gridView1.GetRowCellDisplayText(i, gridView1.Columns["Module"]);            
            var SortNum = gridView1.GetRowCellDisplayText(i, gridView1.Columns["SortNum"]);
            moduleText.Text = Module;
            addrTxt.Text = addr;
            ReadAddrTxt.Text = ReadAddr;
            RearNoteTxt.Text = RearNote;
            PreNoteTxt.Text = PreNote;
            idLab.Text = id;
            SortNumTxt.Text = SortNum;
            simpleButton2.Enabled = true;
            simpleButton1.Enabled = true;
        }
        /// <summary>
        /// 更新或者插入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            parameter_export model = new parameter_export();
            if (string.IsNullOrEmpty(idLab.Text)) //插入
            {
                //Pe_dal.Insert(model);
            }
            else {//更新

                model.addr = addrTxt.Text;
                model.ReadAddr = ReadAddrTxt.Text;
                model.RearNote = RearNoteTxt.Text;
                model.PreNote = PreNoteTxt.Text;
                model.id =int.Parse( idLab.Text);
                model.Module = moduleText.Text;
                model.SortNum = int.Parse(SortNumTxt.Text);
                Pe_dal.Update(model);
            }
            gridControl1.DataSource = Pe_dal.GetList().OrderBy(c => c.SortNum);
            newRefreshDate();
        }

        private void simpleButton2_Click(object sender, EventArgs e)//插入
        {
            parameter_export model = new parameter_export();
            model.addr = addrTxt.Text;
            model.ReadAddr = ReadAddrTxt.Text;
            model.RearNote = RearNoteTxt.Text;
            model.PreNote = PreNoteTxt.Text;
            model.Module = moduleText.Text;
            model.SortNum = int.Parse(SortNumTxt.Text);
            Pe_dal.Insert(model);
            gridControl1.DataSource = Pe_dal.GetList().OrderBy(c => c.SortNum);
            newRefreshDate();
        }

        private void textEdit1_EditValueChanged(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textEdit1.Text))
            {
                gridControl1.DataSource = Pe_dal.GetList().OrderBy(c => c.SortNum);
            }
            else
            {
                gridControl1.DataSource = Pe_dal.GetList(c => c.addr.Contains(textEdit1.Text) || c.Module.Contains(textEdit1.Text) || c.ReadAddr.Contains(textEdit1.Text) || c.RearNote.Contains(textEdit1.Text) || c.PreNote.Contains(textEdit1.Text)).ToList().OrderBy(c=>c.SortNum);
            }
            newRefreshDate();
        }
        private void newRefreshDate()
        {
            var curList = gridControl1.DataSource;
            if (string.IsNullOrEmpty(textEdit1.Text))//搜索为空
            {
                   curList = Pe_dal.GetList().OrderBy(c => c.SortNum);
            }
            else//搜索不为空
            {
                curList = Pe_dal.GetList(c => c.addr.Contains(textEdit1.Text) || c.ReadAddr.Contains(textEdit1.Text) || c.RearNote.Contains(textEdit1.Text) || c.PreNote.Contains(textEdit1.Text)||c.Module.Contains(textEdit1.Text)).OrderBy(c=>c.SortNum);
            }
            gridControl1.DataSource = curList;
            gridView1.RefreshData();

            gridView1.TopRowIndex = topIndex;
            simpleButton1.Enabled = false;
        }

        private void gridView1_TopRowChanged(object sender, EventArgs e)
        {
            topIndex = gridView1.TopRowIndex;
        }
    }
}
